﻿#if !MONOGAME && !XBOX

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Brain;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.IO;
using Brain.ModelExporter;
using Brain.Rendering;

namespace Demos
{
    public class ModelImporterScreen : DemoScreen
    {
        Effect Effect;
        public ModelImporterScreen()
        {
            Viewport viewport = Engine.Instance.GraphicsDevice.Viewport;

            InputFreeCamera3D Camera = new InputFreeCamera3D(this);
            Camera.AspectRatio = viewport.AspectRatio;
            Camera.FarPlane = 1000f;
            //Camera.DegreesRotation = new Vector3(-52, -24, 0);
            //Camera.Position = new Vector3(-43, 59, 165);
            Camera.InitialPosition = Camera.Position;
            Camera.InitialRotationRadians = Camera.RotationRadians;
            Engine.Instance.CameraManager.Camera3D = Camera;

            WorldObject ground = new WorldObject("Game\\Models\\model_box");
            ground.Rendering = WorldObjectRendering.Rendered;
            ground.Position = new Vector3(0, -2, 0);
            ground.Scale = new Vector3(100, 1, 100);
            ground.Draw();

            using (FileStream stream = new FileStream("Content\\Game\\Models\\tesla.bm", FileMode.Open))
            {
                BrainModelContent model = BrainModelReader.Read(stream);
                BrainModel m = new BrainModel();
                m.Meshes = new BrainModelMesh[model.Meshes.Length];
                
                for (int i = 0; i < model.Meshes.Length; i++)
                {
                    BrainModelMeshContent mesh = model.Meshes[i];

                    VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[mesh.Vertices.Count];
                    Vector3[] ver = new Vector3[mesh.Vertices.Count];
                    for (int x = 0; x < mesh.Vertices.Count; x++)
                    {
                        BrainModelVertexContent vertex = mesh.Vertices[x];
                        VertexPositionNormalTexture v = new VertexPositionNormalTexture();
                        v.Position = vertex.Position;
                        v.TextureCoordinate = vertex.TexCoord;
                        //v.Normal = vertex.Normal;

                        vertices[x] = v;
                        ver[x] = vertex.Position;
                    }

                    VertexBuffer vBuffer = new VertexBuffer(Engine.Instance.GraphicsDevice, VertexPositionNormalTexture.VertexDeclaration,
                        vertices.Length, BufferUsage.None);
                    vBuffer.SetData(vertices);

                    int[] indices = mesh.Indices.ToArray();
                    IndexBuffer iBuffer = new IndexBuffer(Engine.Instance.GraphicsDevice, IndexElementSize.ThirtyTwoBits, indices.Length, BufferUsage.None);
                    iBuffer.SetData(indices);

                    BrainModelMesh me = new BrainModelMesh();
                    me.VertexBuffer = vBuffer;
                    me.IndexBuffer = iBuffer;

                    BasicEffect fx = new BasicEffect(Engine.Instance.GraphicsDevice);
                    //fx.EnableDefaultLighting();
                    //fx.PreferPerPixelLighting = true;

                    me.Effect = fx;

                    this.Effect = me.Effect;
                    me.NumVertices = vBuffer.VertexCount;
                    me.PrimitiveCount = iBuffer.IndexCount / 3;
                    me.StartIndex = 0;
                    me.VertexOffset = 0;

                    m.Meshes[i] = me;

                    /*BrainModelWriter writer = new BrainModelWriter();
                    writer.AddNewMesh();
                    for (int x = 0; x < mesh.Vertices.Count; x++)
                    {
                        BrainModelVertexContent vert = mesh.Vertices[x];
                        writer.SubmitPosition(x, vert.Position.X, vert.Position.Y, vert.Position.Z);
                    }

                    for (int x = 0; x < mesh.Indices.Count; x++)
                    {
                        int indice = mesh.Indices[x];
                        writer.SubmitIndice(indice);
                    }

                    writer.MakeMiracles();*/
                }

                WorldObject wo = new WorldObject();
                wo.Model = m;
                wo.Rendering = WorldObjectRendering.Rendered;
                wo.Update();
                wo.Draw();
            }
        }

        public override void Draw()
        {
            base.Draw();

            /*Effect.Parameters["World"].SetValue(Matrix.Identity);
            Effect.Parameters["View"].SetValue(Engine.Instance.CameraManager.Camera3D.View);
            Effect.Parameters["Projection"].SetValue(Engine.Instance.CameraManager.Camera3D.Projection);*/
        }
    }
}

#endif